package com.schoolcloud.common.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Data
public class PageDTO<T> {

    private long pageNum;
    private long total;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonSerialize(nullsUsing = EmptyListSerializer.class)
    private List<T> list;

    // 自定义序列化器，将 null 转换为空数组
    public static class EmptyListSerializer extends StdSerializer<List<?>> {

        public EmptyListSerializer() {
            this(null);
        }

        public EmptyListSerializer(Class<List<?>> t) {
            super(t);
        }

        @Override
        public void serialize(List<?> value, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider provider) throws IOException {
            gen.writeObject(Collections.emptyList());
        }
    }
    public static  PageDTO emptyPage(){
        PageDTO<Object> dto = new PageDTO<>();
        dto.setList(new ArrayList<>());
        dto.setTotal(0);
        return dto;
    }
}
